package com.jyniubi.core.controller.api;


import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.jyniubi.base.exception.Assert;
import com.jyniubi.base.result.R;
import com.jyniubi.base.result.ResponseEnum;
import com.jyniubi.base.utils.JwtUtils;
import com.jyniubi.common.utils.RegexValidateUtils;
import com.jyniubi.core.pojo.entity.UserAccount;
import com.jyniubi.core.pojo.entity.UserInfo;
import com.jyniubi.core.pojo.entity.UserLoginRecord;
import com.jyniubi.core.pojo.vo.UserIndexVO;
import com.jyniubi.core.pojo.vo.UserInfoVo;
import com.jyniubi.core.pojo.vo.UserResponseVO;
import com.jyniubi.core.service.UserAccountService;
import com.jyniubi.core.service.UserInfoService;
import com.jyniubi.core.service.UserLoginRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author jyzhang
 * @since 2024-04-27
 */
@Api(tags = "用户基本信息")

@RestController
@RequestMapping("/api/core/userInfo")
@Slf4j
public class ApiUserInfoController {
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private UserAccountService userAccountService;
    @Resource
    private UserLoginRecordService userLoginRecordService;
    @ApiOperation("用户个人中心首页数据")
    @GetMapping("/auth/userIndex")
    public R getUserIndexVO(@RequestHeader(value = "token",required = false) String token){
        // JwtUtils.getUserId()里面已经封装好了tokne的判空处理，所以直接获取就行
        Long userId = JwtUtils.getUserId(token);
        // 用户个人中心首页数据vo类
        UserIndexVO userIndexVO = new UserIndexVO();
        // 查询数据库获取对应的数据，然后将数据赋值给vo类
        UserInfo userInfo = userInfoService.getOne(Wrappers.lambdaQuery(UserInfo.class).eq(UserInfo::getId, userId));
        userIndexVO.setUserType(userInfo.getUserType());
        userIndexVO.setName(userInfo.getName());
        userIndexVO.setBindStatus(userInfo.getBindStatus());
        userIndexVO.setHeadImg(userInfo.getHeadImg());
        UserAccount userAccount = userAccountService.getOne(Wrappers.lambdaQuery(UserAccount.class).eq(UserAccount::getUserId, userId));
        userIndexVO.setAmount(userAccount.getAmount());
        userIndexVO.setFreezeAmount(userAccount.getFreezeAmount());
        UserLoginRecord userLoginRecord = userLoginRecordService.getOne(Wrappers.lambdaQuery(UserLoginRecord.class)
                .eq(UserLoginRecord::getUserId, userId)
                .select(UserLoginRecord::getCreateTime)
                .orderByDesc(UserLoginRecord::getCreateTime)
                .last("limit 1"));
        userIndexVO.setLastLoginTime(userLoginRecord.getCreateTime());
        return R.ok().data("item",userIndexVO);
    }
    @ApiOperation("用户注册")
    @PostMapping("/register")
    public R register(@RequestBody UserInfoVo userInfoVo){
        String code = userInfoVo.getCode();
        String mobile = userInfoVo.getMobile();
        String password = userInfoVo.getPassword();
        Integer userType = userInfoVo.getUserType();
        //判断参数是否为空和手机号格式是否正确
        Assert.assertNotTrue(StringUtils.isNotBlank(code), ResponseEnum.CODE_NULL_ERROR);
        Assert.assertNotTrue(StringUtils.isNotBlank(mobile),ResponseEnum.MOBILE_NULL_ERROR);
        Assert.assertNotTrue(RegexValidateUtils.checkCellphone(mobile),ResponseEnum.MOBILE_ERROR);
        Assert.assertNotTrue(StringUtils.isNotBlank(password),ResponseEnum.PASSWORD_NULL_ERROR);
        Assert.assertNotTrue(null!=userType,ResponseEnum.USER_TYPE_ERROR);
        userInfoService.register(userInfoVo);
        return R.ok();
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public R login(@RequestBody UserInfoVo userInfoVo, HttpServletRequest request){
        String mobile = userInfoVo.getMobile();
        String password = userInfoVo.getPassword();
        Integer userType = userInfoVo.getUserType();
        //判断参数是否为空和手机号格式是否正确
        Assert.assertNotTrue(StringUtils.isNotBlank(mobile),ResponseEnum.MOBILE_NULL_ERROR);
        Assert.assertNotTrue(RegexValidateUtils.checkCellphone(mobile),ResponseEnum.MOBILE_ERROR);
        Assert.assertNotTrue(StringUtils.isNotBlank(password),ResponseEnum.PASSWORD_NULL_ERROR);
        Assert.assertNotTrue(null!=userType,ResponseEnum.USER_TYPE_ERROR);
        UserResponseVO userResponseVO = userInfoService.login(userInfoVo,request);
        return R.ok().data("userInfo",userResponseVO);
    }
    @ApiOperation("检查token是否过期")
    @GetMapping("/auth/checkToken")
    public R checkToken(@RequestHeader(value = "token",required = false) String token){
        boolean b = JwtUtils.checkToken(token);
        if (b == true){
            log.info("token没有过期");
        } else {
            log.info("token已过期");
        }
        return R.ok().data("flag",b);
    }

    @ApiOperation("手机号重复注册")
    @GetMapping("/checkMobile/{phone}")
    public R checkMobile(@PathVariable("phone") String phone){
        int count = userInfoService.count(Wrappers.lambdaQuery(UserInfo.class).eq(UserInfo::getMobile, phone));
        return R.ok().data("flag",count == 0);
    }
}

